package com.weavernorth.action.humanresource;

import com.weavernorth.sysap.dao.SyncOrgStrucDao;
import com.weavernorth.sysap.dao.SyncOrgStrucDaoImpl;
import com.weavernorth.util.Logger4jUtil;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * 在人事入职流程节点后生成人员到OA人员表,并更新到表单
 *
 * @author zm
 * @version V1.0 2019-12-03 created
 */
public class CreateHrmAction extends BaseBean implements Action {
    private Logger log = Logger4jUtil.getLoggerByName("/action/humanresource/CreateHrmAction/");
    /**
     * 流程路径节点后选择aciton后,会在节点提交后执行此方法。
     */
    @Override
    public String execute(RequestInfo request) {
        log.info("新入职人员Action开始：");
        //根据填写表单字段将信息插入表
        //数据库操作
        try {
            RecordSet rsSelect = new RecordSet();
            RecordSet rsHrmSelect = new RecordSet();
            RecordSet rsUpdate = new RecordSet();
            BaseBean bb = new BaseBean();
            //获取最大人员id
            SyncOrgStrucDao sosDao = new SyncOrgStrucDaoImpl();
            //当前流程表单名称
            String strTable = Util.null2String(request.getRequestManager().getBillTableName());
            //当前流程id
            String strRequestid = Util.null2String(request.getRequestid());
            log.info("当前流程表单名称" + strTable + ";当前流程id:" + strRequestid);
            //个人电话
            String grdh = "";
            //得到人员最大id
            int strMaxid = sosDao.getHrmMaxid();
            log.info("最大id" + strMaxid);

            //查询当前表信息
            String strSql = "select grdh  from  " + strTable + " where requestid='" + strRequestid + "'";
            rsSelect.execute(strSql);
            if (rsSelect.next()) {
                //个人电话
                grdh = Util.null2String(rsSelect.getString("grdh"));
                log.info("当前个人电话" + grdh);
                //查询人员表是否已存在,存在则不插入
                String strHrmSql = "select id from Hrmresource where mobile='" + grdh + "'";
                log.info("查询是否存在："+strHrmSql);
                rsHrmSelect.executeQuery(strHrmSql);
                String ids="";
                while(rsHrmSelect.next()) {
                    //如果表中已存在，则将手机号设置为空
                    ids = Util.null2String(rsHrmSelect.getString("id"));
                    if(!"".equals(ids)) {
                        Integer id = Util.getIntValue(ids);
                        String strUpdateSql = "update Hrmresource set mobile='' where id=" + id + "";
                        log.info("如果存在,更新手机号为空：" + strUpdateSql);
                        RecordSet rs = new RecordSet();
                        rs.executeUpdate(strUpdateSql);
                    }
                }
                    String strInsertSql = "insert into Hrmresource (id,mobile,lastname,status) values (?,?,?,?)";
                    log.info("如果不存在,插入数据："+strInsertSql);
                    log.info("最大id:"+strMaxid+";手机号："+grdh+";");
                    rsUpdate.executeUpdate(strInsertSql, strMaxid, grdh, "入职人员", "1");
            }
            //将当前id更新到流程表单
            String strUpdateSql2 = "update " + strTable + " set rzryid=? where requestid=?";
            log.info("更新流程表单："+strUpdateSql2);
            rsUpdate.executeUpdate(strUpdateSql2, strMaxid, strRequestid);
        }catch (Exception ex){
            log.info("程序异常,异常信息："+ex.toString());
            return FAILURE_AND_CONTINUE;
        }
        return SUCCESS;
    }
}
